package com.kintreda.ieco_server.module.system.web;

import com.kintreda.ieco_server.bean.system.City;
import com.kintreda.ieco_server.core.web.R;
import com.kintreda.ieco_server.module.system.service.ICityService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * @Created by IntelliJ IDEA
 * @Project_Name ieco_server
 * @Package com.kintreda.ieco_server.module.system.web
 * @Author ZhongYao_H
 * @Date 2019/5/21:18:15
 * @Year 2019 Month 05 Day 21
 * @Date_zh 2019 年 五月 星期二
 */
@Controller
@RequestMapping("/city")
public class CityController {
    @Autowired
    ICityService cityService;

    @RequestMapping("/list")
    public ModelAndView citylist(){
        return new ModelAndView("city/city-list");
    }

    @RequestMapping(value = "/query",method = RequestMethod.POST)
    @ResponseBody
    public Object query(String name){

        if (StringUtils.isNotBlank(name)){
            return cityService.query("from City where name like ? ",new Object[]{name});
        }else {
            return cityService.query("from City ",null);
        }

    }

    @RequestMapping("/add")
    public ModelAndView add(){
        return new ModelAndView("city/city-add");
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public Object citylist(City city){
        if (cityService.isadd(city.getName())){
            return R.error("此名称的城市以添加!");
        }
        cityService.save(city);
        return R.ok("添加成功!");
    }

    @RequestMapping(value = "/del",method = RequestMethod.POST)
    @ResponseBody
    public Object citylist(String id){
        cityService.deleteByPK(id);
        return R.ok("删除成功!");
    }

}
